package com.cbb.display.config.quartz;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.DateBuilder;
import org.quartz.DateBuilder.IntervalUnit;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cbb.display.entity.SysTask;

/** 

* @author 作者 lujunjie: 

* @version 创建时间：Jan 18, 2020 11:31:31 AM 

* 类说明 

*/
@Component
public class QuartzManager {
  // private SchedulerFactoryBean schedulerFactoryBean
  // =SpringContextHolder.getBean(SchedulerFactoryBean.class);
  // @Autowired
  // @Qualifier("schedulerFactoryBean")
  // private SchedulerFactoryBean schedulerFactoryBean;
  @Autowired
  private Scheduler scheduler;
 
  /**
   * 添加任务
   * 
   * @param scheduleJob
   * @throws SchedulerException
   */
   
  public void addJob(SysTask job) {
    try {
      // 创建jobDetail实例，绑定Job实现类
      // 指明job的名称，所在组的名称，以及绑定job类
      Class<? extends Job> jobClass = (Class<? extends Job>) (Class.forName(job.getBeanClass()).newInstance()
          .getClass());
      JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(job.getJobName(), job.getJobGroup())// 任务名称和组构成任务key
          .build();
      // 定义调度触发规则
      // 使用cornTrigger规则
      Trigger trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup())// 触发器key
          .startAt(DateBuilder.futureDate(1, IntervalUnit.SECOND))
          .withSchedule(CronScheduleBuilder.cronSchedule(job.getCronExpression())).startNow().build();
      // 把作业和触发器注册到任务调度中
      scheduler.scheduleJob(jobDetail, trigger);
      // 启动
      if (!scheduler.isShutdown()) {
        scheduler.start();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
// public void addJob(ScheduleJob job) throws SchedulerException {
//   if (job == null || !ScheduleJob.STATUS_RUNNING.equals(job.getJobStatus())) {
//     return;
//   }
//
//   TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup());
//
//   CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
//
//   // 不存在，创建一个
//
//   if (null == trigger) {
//     Class<? extends Job> clazz = ScheduleJob.CONCURRENT_IS.equals(job.getIsConcurrent())
//         ? QuartzJobFactory.class
//         : QuartzJobFactoryDisallowConcurrentExecution.class;
//
//     JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(job.getJobName(), job.getJobGroup()).build();
//
//     jobDetail.getJobDataMap().put("scheduleJob", job);
//
//     CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
//
//     trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup())
//         .withSchedule(scheduleBuilder).build();
//
//     scheduler.scheduleJob(jobDetail, trigger);
//   } else {
//     // Trigger已存在，那么更新相应的定时设置
//
//     CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
//
//     // 按新的cronExpression表达式重新构建trigger
//
//     trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
//
//     // 按新的trigger重新设置job执行
//
//     scheduler.rescheduleJob(triggerKey, trigger);
//   }
// }
 
  /**
   * 获取所有计划中的任务列表
   * 
   * @return
   * @throws SchedulerException
   */
  public List<SysTask> getAllJob() throws SchedulerException {
    GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
    Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);
    List<SysTask> jobList = new ArrayList<SysTask>();
    for (JobKey jobKey : jobKeys) {
      List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
    
      for (Trigger trigger : triggers) {
    	Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
    	SysTask job = SysTask.builder()
    			.jobName(jobKey.getName())
    			.jobGroup(jobKey.getGroup())
    			.description("触发器:" + trigger.getKey())
    			.jobStatus(triggerState.name())
    			.build();
        if (trigger instanceof CronTrigger) {
          CronTrigger cronTrigger = (CronTrigger) trigger;
          String cronExpression = cronTrigger.getCronExpression();
          job.setCronExpression(cronExpression);
        }
        jobList.add(job);
      }
    }
    return jobList;
  }
 
  /**
   * 所有正在运行的job
   * 
   * @return
   * @throws SchedulerException
   */
  public List<SysTask> getRunningJob() throws SchedulerException {
    List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
    List<SysTask> jobList = new ArrayList<SysTask>(executingJobs.size());
    for (JobExecutionContext executingJob : executingJobs) {
      JobDetail jobDetail = executingJob.getJobDetail();
      JobKey jobKey = jobDetail.getKey();
      Trigger trigger = executingJob.getTrigger();
      Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
      SysTask job = SysTask.builder()
  			.jobName(jobKey.getName())
  			.jobGroup(jobKey.getGroup())
  			.description("触发器:" + trigger.getKey())
  			.jobStatus(triggerState.name())
  			.build();
      if (trigger instanceof CronTrigger) {
        CronTrigger cronTrigger = (CronTrigger) trigger;
        String cronExpression = cronTrigger.getCronExpression();
        job.setCronExpression(cronExpression);
      }
      jobList.add(job);
    }
    return jobList;
  }
 
  /**
   * 暂停一个job
   * 
   * @param scheduleJob
   * @throws SchedulerException
   */
  public void pauseJob(SysTask scheduleJob) throws SchedulerException {
    JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
    scheduler.pauseJob(jobKey);
  }
 
  /**
   * 恢复一个job
   * 
   * @param scheduleJob
   * @throws SchedulerException
   */
  public void resumeJob(SysTask scheduleJob) throws SchedulerException {
	TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
    CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
    JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
    if(trigger==null)
    {
    	addJob(scheduleJob);
    }
    else {
    	scheduler.resumeJob(jobKey);
	}
    
  }
 
  /**
   * 删除一个job
   * 
   * @param scheduleJob
   * @throws SchedulerException
   */
  public void deleteJob(SysTask scheduleJob) throws SchedulerException {
    JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
    scheduler.deleteJob(jobKey);
 
  }
 
  /**
   * 立即执行job
   * 
   * @param scheduleJob
   * @throws SchedulerException
   */
  public void runAJobNow(SysTask scheduleJob) throws SchedulerException {
    JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
    scheduler.triggerJob(jobKey);
  }
 
  /**
   * 更新job时间表达式
   * 
   * @param scheduleJob
   * @throws SchedulerException
   */
  public void updateJobCron(SysTask scheduleJob) throws SchedulerException {
 
    TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
 
    CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
 
    CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression());
 
    trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
 
    scheduler.rescheduleJob(triggerKey, trigger);
  }
}